package com.example.first;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class RateListActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {
    public static final String TAG="Click";
    ListView listView;
    MyAdapter adapter;
    private ProgressBar bar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);
        //listView=findViewById(R.id.mylist2);
        bar =findViewById(R.id.progressBar2);
        //页面无数据时显示加载中
        //listView.setEmptyView(bar);
        //初始化数据
        ArrayList<HashMap<String,String>> listItems=new ArrayList<HashMap<String,String>>();
        for (int i=0;i<30;i++){
            HashMap<String,String> map =new HashMap<String,String>();
            map.put("ItemTitle","Rate:"+i);
            map.put("ItemDetail","detail"+i);
            listItems.add(map);
        }
        //生成适配器的Item和动态数组对应的元素
        SimpleAdapter listItemAdapter=new SimpleAdapter(this,
                listItems,
                R.layout.list_item,
                new String[]{"ItemTitle","ItemDetail"},
                new int[]{R.id.title,R.id.detail}
        );
        //绑定控件
        listView =findViewById(R.id.mylist2);
        listView.setAdapter(listItemAdapter);
        listView.setOnItemClickListener(this);
       // listView.setEmptyView(findViewById(R.id.nodata));
        listView.setOnItemLongClickListener(this);

        Handler handler=new Handler(Looper.myLooper()) {
            public void handleMessage(Message msg) {
                if (msg.what == 7) {
                    ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    //构造Adapter
                    /*SimpleAdapter listItemAdapter=new SimpleAdapter(RateListActivity2.this,
                            list,
                            R.layout.list_item,
                            new String[]{"cname","cval"},
                            new int[]{R.id.title,R.id.detail}
                    );
                    listView.setAdapter(listItemAdapter);*/
                    adapter=new MyAdapter(RateListActivity2.this,R.layout.list_item,list);
                    listView.setAdapter(adapter);
                    //隐藏bar
                    bar.setVisibility(View.INVISIBLE);
                }
                super.handleMessage(msg);
            }
        };
        MapTask task=new MapTask();
        task.setHandler(handler);
        Thread t= new Thread(task);
        t.start();
    }
    public void onItemClick(AdapterView<?> parent,View view,int position,long id){
        Log.i(TAG, "onItemClick: onItemClick");
        //获取数据项
        HashMap<String,String> map=(HashMap<String,String>)listView.getItemAtPosition(position);
        Log.i(TAG,"onItemClick:cname="+map.get("cname"));
        Log.i(TAG,"onItemClick:cval="+map.get("cval"));
        //删除当前选择的元素
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setMessage("请确认是否删除当前数据")
                .setPositiveButton("是",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,int which){
                        Log.i(TAG,"onClick:对话框事件处理");
                        //删除数据项
                        adapter.remove(listView.getItemAtPosition(position));
                    }
                }).setNegativeButton("否",null);
        builder.create().show();
        String titleStr=map.get("cname");
        String detailStr=map.get("cval");
        TextView title=(TextView) view.findViewById(R.id.title);
        TextView detail=(TextView) view.findViewById(R.id.detail);
        String title2=String.valueOf(title.getText());
        String detail2=String.valueOf(detail.getText());
        float detail3=Float.valueOf(detail2);
        Intent intent=new Intent(this,ConfigActivity2.class);
        intent.putExtra("name1",title2);
        intent.putExtra("rate1",detail3);
        startActivity(intent);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
        Log.i(TAG,"onItemLongClick:pos="+position);
        return true;
    }
}